package com.hui.account.utils;

import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.hui.account.R;
import com.hui.account.entity.custom.CustomKeyBoardView;

/**
 * 自定义键盘
 */
public class KeyBoardUtils {

    private final Keyboard keyboard;

    private CustomKeyBoardView keyboardView;

    private EditText moneyEdit;

    public interface OnEnsureListener {
        public void onEnsure();
    }

    OnEnsureListener onEnsureListener;

    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    public KeyBoardUtils(CustomKeyBoardView keyboardView, EditText moneyEdit) {
        this.keyboardView = keyboardView;
        this.moneyEdit = moneyEdit;
        this.moneyEdit.setInputType(InputType.TYPE_NULL);
        keyboard = new Keyboard(this.moneyEdit.getContext(), R.xml.key);

        this.keyboardView.setKeyboard(keyboard);
        this.keyboardView.setEnabled(true);
        this.keyboardView.setPreviewEnabled(false);
        this.keyboardView.setOnKeyboardActionListener(listener);
    }

    KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void onPress(int i) {

        }

        @Override
        public void onRelease(int i) {

        }

        @Override
        public void onKey(int i, int[] ints) {
            Editable text = moneyEdit.getText();
            int start = moneyEdit.getSelectionStart();
            if(0 == start && 46 == i)
                return;

            if(!TextUtils.isEmpty(text) && text.toString().contains(".") && i == 46)
                return;

            switch (i) {
                case Keyboard.KEYCODE_DELETE:
                    if (null != text && text.length() > 0) {
                        if (start > 0) {
                            text.delete(start - 1, start);
                        }
                    }
                    break;
                case Keyboard.KEYCODE_CANCEL:
                    text.clear();
                    break;
                case Keyboard.KEYCODE_DONE:
                    onEnsureListener.onEnsure();
                    break;
                default:
                    text.insert(start, Character.toString((char) i));
                    break;
            }
        }

        @Override
        public void onText(CharSequence charSequence) {

        }

        @Override
        public void swipeLeft() {

        }

        @Override
        public void swipeRight() {

        }

        @Override
        public void swipeDown() {

        }

        @Override
        public void swipeUp() {

        }
    };

    /**
     * 显示软键盘
     */
    public void showKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.INVISIBLE || visibility == View.GONE) {
            keyboardView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 隐藏软键盘
     */
    public void hideKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.VISIBLE || visibility == View.INVISIBLE) {
            keyboardView.setVisibility(View.GONE);
        }
    }
}
